package com.example.demo.model.index.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.Results.ResultClass;
import com.example.demo.model.goods.goods.entity.ShopGoods;
import com.example.demo.model.goods.goods.entity.ShopGoodsType;
import com.example.demo.model.goods.goods.mapper.ShopGoodsDao;
import com.example.demo.model.goods.goods.mapper.ShopGoodsTypeDao;
import com.example.demo.model.index.index.entity.ShopCartReq;
import com.example.demo.model.index.index.service.IndexService;
import com.example.demo.model.user.user.entity.ShopUser;
import com.example.demo.model.user.user.mapper.ShopUserDao;
import com.example.demo.model.user.user.mapper.UserAddressDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    ShopUserDao shopUserDao;
    @Autowired
    ShopGoodsTypeDao shopGoodsTypeDao;
    @Autowired
    UserAddressDao userAddressDao;
    @Autowired
    ShopGoodsDao shopGoodsDao;

    @Override
    public ModelAndView login() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/index/login");
        return modelAndView;


    }

    @Override
    public ResultClass doLogin(HttpServletRequest request, ShopUser shopUser) {
        String md5Password = DigestUtils.md5DigestAsHex(shopUser.getUserPass().getBytes());
        shopUser.setUserPass(md5Password);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name", shopUser.getUserName());
        queryWrapper.eq("user_pass", shopUser.getUserPass());

        List<ShopUser> list = shopUserDao.selectList(queryWrapper);

        if (list.size() != 1) {
            return ResultClass.fail("用户名密码错误");
        }
        request.getSession().setAttribute("userId", list.get(0).getId());
        request.getSession().setAttribute("userName", list.get(0).getUserName());

        return ResultClass.ok("登录成功");
    }

    @Override
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/index/index");
        List<ShopGoodsType> shopGoodsTypeList = shopGoodsTypeDao.selectList(null);
        modelAndView.addObject("shopGoodsTypeList", shopGoodsTypeList);
        return modelAndView;
    }

    @Override
    public ModelAndView shopCart(Integer addressId) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/index/shopCart");
        if (addressId != null) {
            String address = userAddressDao.selectById(addressId).getAddress();
            modelAndView.addObject("address", address);
        }
        return modelAndView;
    }

    @Override
    public ResultClass<List<ShopGoods>> shopCartGet(List<ShopCartReq> shopCartReqs) {
        List<Integer> goodsIdList = shopCartReqs.stream()
                .map(ShopCartReq::getId)
                .distinct()//去重
                .collect(Collectors.toList());

        QueryWrapper<ShopGoods> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().in(ShopGoods::getId, goodsIdList);
        List<ShopGoods> list = shopGoodsDao.selectList(queryWrapper);
        return ResultClass.ok(list);
    }
}
